package com.atguigu.yygh.msm.service.impl;



import com.atguigu.yygh.msm.service.MsmService;
import com.atguigu.yygh.msm.utils.HttpUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.apache.http.HttpResponse;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class MsmServiceImpl implements MsmService {


    //根据手机号发送验证码
    @Override
    public boolean send(String phone, String code) {
        System.out.println(phone);
        System.out.println(code);
        if (StringUtils.isBlank(phone)) {
            return false;
        }
        String method = "post";
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "APPCODE " + "http://dingxin.market.alicloudapi.com");
        Map<String, String> querys = new HashMap<String, String>();
        // 手机号
        querys.put("mobile", phone);
        // 验证码
        querys.put("param", "code:" + code);
        // 模板id
        querys.put("tpl_id", "TP1711063");
        Map<String, String> bodys = new HashMap<String, String>();
        try {
            HttpResponse response = HttpUtils.doPost("http://dingxin.market.alicloudapi.com",
                        "/dx/sendSms", method, headers, querys, bodys);
            return true;
//            System.out.println(response.toString());
            //获取response的body
            //System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
